package br.com.ufpr.sgp.util.geral;

import org.apache.wicket.Application;
import org.apache.wicket.Component;
import org.apache.wicket.behavior.Behavior;
import org.apache.wicket.markup.head.IHeaderResponse;
import org.apache.wicket.markup.head.JavaScriptHeaderItem;
import org.apache.wicket.markup.head.OnDomReadyHeaderItem;
import org.apache.wicket.request.resource.JavaScriptResourceReference;

/**
 * http://digitalbush.com/projects/masked-input-plugin/
 * 
 * @author Willian Tanaka
 */
public class MaskedInputBehavior extends Behavior {
    private static final long serialVersionUID = 7060352332385770915L;

    private final String mask;

    public MaskedInputBehavior(final String mask) {
        this.mask = mask;
    }

    @Override
    public void renderHead(final Component component, final IHeaderResponse response) {
        super.renderHead(component, response);
        response.render(JavaScriptHeaderItem.forReference(Application.get().getJavaScriptLibrarySettings().getJQueryReference()));
        response.render(JavaScriptHeaderItem.forReference(new JavaScriptResourceReference(MaskedInputBehavior.class,
                "jquery.maskedinput.min.js")));
        response.render(OnDomReadyHeaderItem.forScript("$(document).ready(function(){$(\"#" + component.getMarkupId() + "\").mask(\""
                + mask + "\");})"));
    }

}
